package com.irdstudio.biz.drools.common;

import org.apache.commons.lang.StringUtils;
import org.kie.api.runtime.rule.AccumulateFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

/**
 * 征信还款状态 连续
 */
public class RepayStatusContinuousAccumulate extends RepayStatusBaseAccumulate {

    public void accumulate(Serializable context, Object value) {
        SumData data = (SumData) context;
        Object[] args = (Object[]) value;
        String repayStatus = (String) args[0];
        String months = (String) args[1];
        int month = Integer.valueOf(months);
        int count = 0;
        int max = 0;
        if (repayStatus == null) {
            return;
        }
        String[] stats = splitRepayStatus(repayStatus);
        int len = stats.length;
        month = len - month;
        // 从后往前
        for (int i = len - 1; i >= month && i >= 0; i--) {
            if (stats[i].matches("\\d{1}")) {
                count++;
            } else {
                // N / * normal
                if (count > max) {
                    max = count;
                }
                count = 0; // 重新统计
            }
        }
        data.total = Math.max(max, data.total);
        log.debug("RepayStatusContinuousAccumulate accumulate {}, cuurent total {}", max, data.total);
    }
}
